package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.adapter.NewsAdapter;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.News;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Sousuo extends AppCompatActivity {
    RecyclerView recyclerView;
    String title;
    String responseData;
    List<News> newsList = new ArrayList<>();
    NewsAdapter newsAdapter;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    if(newsList!=null) {
                        recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL));
                        newsAdapter = new NewsAdapter(newsList, getApplicationContext());
                        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        recyclerView.setAdapter(newsAdapter);
                    }else {
                        Toast.makeText(getApplicationContext(),"查询结果为空",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sousuo);
        Toolbar toolbar=findViewById(R.id.sousuo_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("搜索结果");
        recyclerView=findViewById(R.id.sousuo_news);
        Intent intent=getIntent();
        title=intent.getStringExtra("news_title");
        okhttp();
    }

    private void okhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest("http://47.101.175.33:8080/AndroidDataFinalTest/news_findByTitle.action?news_title="+title, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        responseData=response.body().string();
                        parseJSONObeject(responseData);
                    }
                });
            }
        }).start();
    }

    private void parseJSONObeject(String responseData) {
        Gson gson=new Gson();
        newsList=gson.fromJson(responseData, new TypeToken<List<News>>() {
        }.getType());
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);
    }
}
